/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-15 11:00:54
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-03 10:39:55
 * @FilePath: \cxl-driving-training\src\views\classManagement\learnDrivingTypeList\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import router from '@/router'
import { ElMessage } from 'element-plus'
import { learnDrivingTypeDisable, learnDrivingTypeList, learnDrivingTypeListInterface } from '@/apis/learnDrivingType'

export default class LearnDrivingTypeData extends BaseData implements BaseMethod {
    search = { schoolId: '', productType: '', status: '' }
    constructor() {
        super(tableConfig)
        const options: { label: string; value: string }[] = []
        this.uStore.queryDrivingSchoolSimple((res: Map<string, any>) => {
            res.forEach(item => {
                options.push({ label: item.shortName, value: item.id })
            })

            tableConfig.search[1].options.push(...options)
        })
        this.getData()
    }
    getData = (arg?: any) => {
        const params: learnDrivingTypeListInterface = {
            size: this.pageData.pagination.pageSize,
            current: this.pageData.pagination.pageNo
        }
        if (this.search.schoolId) {
            params.schoolId = this.search.schoolId
        }
        if (this.search.productType !== '' && this.search.productType !== '-1') {
            params.productType = Number(this.search.productType)
        }
        if (this.search.status !== '' && this.search.status !== '-1') {
            params.disable = Number(this.search.status)
        }
        console.log(params)
        learnDrivingTypeList(params).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total
                const arr: any[] = res.data.result.records

                arr.forEach((element: any) => {
                    element.imageUrl = element.advertisePic
                    // element.disable = Number(element.disable) === 0 ? '启用' : '禁用'
                    element.status = Number(element.disable) === 0 ? true : false
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {
        console.log(arg)
        this.search = arg
        this.getData()
    }
    onReset = (arg?: any) => {
        this.search = { schoolId: '', productType: '', status: '' }
        this.getData()
    }
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type, data)
        if (type === 'create') {
            router.push({ path: '/learnDrivingTypeList/operation', query: { type } })
        } else if (type === 'edit') {
            router.push({ path: '/learnDrivingTypeList/operation', query: { type, id: data.id } })
        } else if (type === 'look') {
            router.push({ path: '/learnDrivingTypeList/operation', query: { type, id: data.id } })
        } else if (type === 'switch') {
            learnDrivingTypeDisable(data.status ? 0 : 1, data.id).then(res => {
                if (Number(res.data.code) === 200) {
                    ElMessage.success('修改成功')
                    this.getData()
                }
            })
        }
    }
}
